﻿#include "SettingsLayer.h"
#include "SysMenu.h"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
//SettingsLayer::SettingsLayer() {
bool SettingsLayer::init() {
	if ( !CCLayer::init() )
	{
		return false;
	}
	//CCSize winSize=CCSizeMake(320,480);
	/*CCSprite *sp = CCSprite::create("loading.png");
	sp->setAnchorPoint(ccp(0,0));
	addChild(sp, 0, 1);*/

	CCSprite *title = CCSprite::create("fengmian.png");
	title->setPosition(ccp(global_winSize.width / 2, 550));
	addChild(title);

	CCMenuItemFont::setFontName("Aria");
	
	CCMenuItemFont::setFontSize(30);
	//std::string text1 = WStrToUTF8("声音");
	CCMenuItemFont * title1 = CCMenuItemFont::create("Sound");
	title1->setEnabled(false);
	//std::string textOn = WStrToUTF8("开");
	//std::string textOff =WStrToUTF8("关");
	//CCMenuItemFont::setFontName("Aria");
	//CCMenuItemFont::setFontSize(26);
	item1 = CCMenuItemToggle::createWithTarget(this, menu_selector(SettingsLayer::soundControl),
		CCMenuItemFont::create("On"), CCMenuItemFont::create("Off"),NULL);
	item1->setColor(ccc3(250,179,0));
	//CCMenuItemFont::setFontName("Aria");
	//CCMenuItemFont::setFontSize(18);
	//std::string textMode = WStrToUTF8("模式");
	CCMenuItemFont* title2 = CCMenuItemFont::create("Mode");
	title2->setEnabled(false);
	//std::string textMode1 = WStrToUTF8("简单");
	//std::string textMode2 = WStrToUTF8("困难");

	//CCMenuItemFont::setFontName("Aria");
	//CCMenuItemFont::setFontSize(26);
	item2 = CCMenuItemToggle::createWithTarget(this, menu_selector(SettingsLayer::modeControl),
		CCMenuItemFont::create("Simple"),
		CCMenuItemFont::create("Difficult"),
		NULL);

	item2->setColor(ccc3(250,179,0));
	//CCMenuItemFont::setFontName("Aria");
	//CCMenuItemFont::setFontSize(26);
	//std::string textBack = WStrToUTF8("返回");
	CCLabelTTF* label = CCLabelTTF::create("Go back", "Aria",30);
	CCMenuItemLabel* back = CCMenuItemLabel::create(label, this,menu_selector(SettingsLayer::backCallback));
	
	label->setColor(ccc3(250,179,0));
	CCMenu *menu = CCMenu::create(title1,  item1,title2,item2, back,NULL);
	//menu->alignItemsInColumns(2, 2, 1);
	menu->alignItemsVerticallyWithPadding(30);

	addChild(menu);
	menu->setPosition(ccp(global_winSize.width / 2,
		global_winSize.height /3));

	if(global_sound)
	{
		item1->setSelectedIndex(0);
	}
	else
	{
		item1->setSelectedIndex(1);
	}
	if(global_difficult)
	{
		item2->setSelectedIndex(1);
	}
	else
	{
		item2->setSelectedIndex(0);
	}
	return true;
}
void SettingsLayer::backCallback(CCObject *pSender) {
	CCScene *scene = CCScene::create();
	scene->addChild(SysMenu::create());
	CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.2, scene));
}
void SettingsLayer::modeControl(CCObject *pSender) {
	int i;
	i=item2->getSelectedIndex();
	if(i==0)
	{
		global_difficult=false;
	}
	else
	{
		global_difficult=true;
	}
	CCUserDefault::sharedUserDefault()->setBoolForKey(ID_DIFF, global_difficult);
}
void SettingsLayer::soundControl(CCObject *pSender) {
	int i;
	i=item1->getSelectedIndex();
	if(i==0)
	{
		global_sound=true;
	}
	else
	{
		global_sound=false;
		SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
		SimpleAudioEngine::sharedEngine()->pauseAllEffects();
	}
	CCUserDefault::sharedUserDefault()->setBoolForKey(ID_SOUND, global_sound);
}
//    soundControl:function(){
//        global.sound = global.sound ? false : true;
//        if(!global.sound){
//            CCAudioManager.sharedEngine().end();
//        }
//    }
//    modeControl:function(){
//    }

//CCLayer* SettingsLayer::node() {
//	SettingsLayer* sg = new SettingsLayer();
//	sg->init();
//	return sg;
//
//}